package com.hspedu.homework;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 使用ArrayList 完成对 对象 Car {name, price} 的各种操作
 * 1.add:添加单个元素
 * 2.remove:删除指定元素
 * 3.contains:查找元素是否存在
 * 4.size:获取元素个数
 * 5.isEmpty:判断是否为空
 * 6.clear:清空
 * 7.addAll:添加多个元素
 * 8.containsAll:查找多个元素是否都存在
 * 9.removeAll：删除多个元素
 * 使用增强for和 迭代器来遍历所有的car , 需要重写 Car 的toString方法
 */
public class Homework02 {
    public static void main(String[] args) {
        List list = new ArrayList();
        Car car = new Car("宝马", 400000);
        Car car2 = new Car("宾利", 5000000);
        //1.add:添加单个元素
        list.add(car);
        list.add(car2);
        //2.remove:删除指定元素
        list.remove(car);
        for (Object o : list) {
            System.out.println(o);
        }

        //3.contains:查找元素是否存在
        System.out.println("car是否存在:" + list.contains(car));
        //4.size:获取元素个数
        System.out.println("list.size() = " + list.size());
        //5.isEmpty:判断是否为空
        System.out.println("list.isEmpty() = " + list.isEmpty());
        //6.clear:清空
//        list.clear();
        for (Object o : list) {
            System.out.println(o);
        }
        //7.addAll:添加多个元素
        list.addAll(1, list);
        //8.containsAll:查找多个元素是否都存在
        System.out.println("list.containsAll(list) = " + list.containsAll(list));
//        Iterator iterator = list.iterator();
//        while (iterator.hasNext()) {
//            Object next = iterator.next();
//            System.out.println(next);
//        }

        //9.removeAll：删除多个元素
        list.removeAll(list);


    }
}

class  Car{
    private String name;
    private int price;

    public Car(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public Car() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}